import { IPriorQueue } from "./types";
import Heap from '@/sources/Heap/maxHeap'


/**
 * 优先级队列（二叉堆）
 */
export default class PriorQueue<T = number> implements IPriorQueue<T> {
    private h: Heap<T>
    constructor(elements: Array<T>, public compartor = (a: T, b: T) => ((a as any) - (b as any))) {
        this.h = new Heap<T>(elements, compartor)
    }


    /**
     * 获取数量
     */
    size() {
        return this.h.size()
    }

    /**
     * 是否为空
     */
    isEmpty() {
        return this.size() === 0
    }

    /**
     * element入队
     * @param element 
     */
    enQueue(element: T) {
        this.h.add(element);
    }

    /**
     * 出队
     */
    deQueue() {
        return this.h.remove()
    }

    /**
     * 获取队头元素
     */
    front() {
        return this.h.get()
    }

    /**
     * 清空队列
     */
    clear() {
        this.h.clear()
    }

    forEach(cb: (e: T, i: number) => void) {
        console.log(this.h.size())
        this.h.forEach(cb)
    }
    
}